package com.bt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsGrouponRules;
import com.bt.service.DtsGrouponRulesService;
import com.bt.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/26 16:05
 **/
@RestController
@RequestMapping("/admin/groupon")
@CrossOrigin
public class AdminGrouponController {
    @Autowired
    private DtsGrouponRulesService dtsGrouponRulesServiceImpl;
    /**
     * http://localhost:8080/admin/groupon/list?page=1&limit=20&goodsId=22&sort=add_time&order=desc
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "查询")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       Integer goodsId,
                        String sort,
                        String order) {
        IPage<DtsGrouponRules> grouponRulesIPage = dtsGrouponRulesServiceImpl.findDtsGrouponrules(goodsId, page, limit, sort, order);
        Map data=new HashMap();
        data.put("total",grouponRulesIPage.getTotal());
        data.put("items",grouponRulesIPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/groupon/create
     * post
     */
    @PostMapping("/create")
    @RequiresPermissions("admin:groupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "添加")
    public Object create(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules.getGoodsId() == null) {
            return ResponseUtil.badArgument();
        }
        try {
            dtsGrouponRulesServiceImpl.insertDtsGroupRules(dtsGrouponRules);
            return ResponseUtil.ok(dtsGrouponRules);

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
    /**
     * http://localhost:8080/admin/groupon/update
     * post
     *
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:groupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "编辑")
    public Object update(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules.getGoodsId() == null) {
            return ResponseUtil.badArgument();
        }
        try {
            dtsGrouponRulesServiceImpl.updateDtsGroupRules(dtsGrouponRules);
            return ResponseUtil.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
    /**
     * http://localhost:8080/admin/groupon/delete
     * post
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:groupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "删除")
    public Object delete(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules.getGoodsId() == null) {
            return ResponseUtil.badArgument();
        }
        try {
            dtsGrouponRulesServiceImpl.deleteDtsGroupRules(dtsGrouponRules);
            return ResponseUtil.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
}
